package com.java.controller;

import com.java.entity.PageBean;
import com.java.entity.PsInspectiontaskMain;
import com.java.entity.RespBean;
import com.java.service.PsInspectiontaskMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * @author caiyuan
 * @version 1.0
 * @Date 2020/6/20 12:56 Dream-Seeker
 * @Description
 */
@RestController
@RequestMapping("/inspectask")
@Scope("prototype")
public class PsInspectiontaskMainController {

    @Autowired
    PsInspectiontaskMainService psInspectiontaskMainService;

    @GetMapping("/allInspectask")
    public RespBean main(@RequestParam(value = "currentPage", defaultValue = "1", required = false) Integer currentPage,
                         @RequestParam(name = "inspectionTaskCode") String inspectionTaskCode,
                         @RequestParam(name = "lineCode") String lineCode,
                         @RequestParam(name = "taskStatusName") String taskStatusName,
                         @RequestParam(name = "issuedByName") String issuedByName,
                         @RequestParam(name = "issuedTimestart") String issuedTimestart,
                         @RequestParam(name = "issuedTimeend") String issuedTimeend) {
        try {
            issuedByName = new String(issuedByName.getBytes("ISO-8859-1"), "UTF-8");
            taskStatusName = new String(taskStatusName.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PageBean<PsInspectiontaskMain> list = psInspectiontaskMainService.findByPageInspec(
                currentPage,
                inspectionTaskCode,
                lineCode,
                taskStatusName,
                issuedByName,
                issuedTimestart,
                issuedTimeend);//回显分页数据
        if (null != list) {
            System.out.println(list);
            return RespBean.ok("查询成功", list);
        } else {
            return RespBean.error("查询失败");
        }
    }

    @GetMapping("/{InspecId}")
    public RespBean getInspecById(@PathVariable Integer InspecId) {
        PsInspectiontaskMain inspec = psInspectiontaskMainService.getInspecById(InspecId);
        if (inspec != null) {
            return RespBean.ok("查询成功", inspec);
        } else {
            return RespBean.error("查询失败");
        }

    }


    @PostMapping("/")
    public RespBean addPsInspec(@RequestBody PsInspectiontaskMain psInspectiontaskMain) {
        System.out.println(psInspectiontaskMain);
        Integer i = psInspectiontaskMainService.addInspection(psInspectiontaskMain);
        System.out.println(i);
        if (i == 1) {
            return RespBean.ok("添加成功", i);
        } else {
            return RespBean.error("添加失败", i);
        }
    }


    @PutMapping("/")
    public RespBean updatePsInspec(@RequestBody PsInspectiontaskMain psInspectiontaskMain) {

        Integer i = psInspectiontaskMainService.updatePsInspection(psInspectiontaskMain);
        if (i == 1) {
            return RespBean.ok("修改成功", i);
        } else {
            return RespBean.error("修改失败", i);
        }
    }


    @DeleteMapping("/{inspeId}")
    public RespBean delInspection(@PathVariable Integer inspeId) {
        Integer i = psInspectiontaskMainService.delInspection(inspeId);
        if (i == 1) {

            return RespBean.ok("取消成功", i);
        } else {
            return RespBean.error("取消失败", i);
        }
    }

    @PutMapping("/{inspeId}")
    public RespBean taskStatusInspection(@PathVariable Integer inspeId) {
        Integer i = psInspectiontaskMainService.taskStatusInspection(inspeId);
        if (i == 1) {
            return RespBean.ok("分配成功", i);
        } else {
            return RespBean.error("分配失败", i);
        }
    }

    @PutMapping("/carriedOut/{inspeId}")
    public RespBean carriedOut(@PathVariable Integer inspeId) {
        Integer i = psInspectiontaskMainService.carriedOutInspection(inspeId);
        if (i == 1) {
            return RespBean.ok("执行成功", i);
        } else {
            return RespBean.error("执行失败", i);
        }
    }


}
